package com.cacshanghai.cac_oa_client.ui;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cacshanghai.cac_oa_client.R;
import com.cacshanghai.cac_oa_client.beans.Meeting;
import com.cacshanghai.cac_oa_client.widget.NoScrollListView;
import com.codetroopers.betterpickers.calendardatepicker.CalendarDatePickerDialogFragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by geyh on 2016/5/26.
 */
public class MeetingActivity extends BaseActivity {

    private static final String FRAG_TAG_DATE_PICKER = "fragment_date_picker_name";

    private NoScrollListView lv_big_room, lv_middle_room, lv_small_room1, lv_small_room2;
    private TextView tv_calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meeting);
        setTitle(getString(R.string.meeting_manager));
        setBack();
        setTitleRightImageView(R.mipmap.plus, addMeeting);

        initBigRoom();
        initMiddleRoom();
        initSmallRoom1();
        initSmallRoom2();

        tv_calendar = (TextView) findViewById(R.id.tv_calendar);
        initDatePicker();

    }

    private void initDatePicker() {
        tv_calendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment()
                        .setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
                            @Override
                            public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
                                try {
                                    String dateFormat = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
                                    Date date = new SimpleDateFormat("yyyy-M-d").parse(dateFormat);
                                    tv_calendar.setText(dateFormat + "(" + getWeekOfDate(date) + ")");
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).setThemeLight();
                cdp.show(getSupportFragmentManager(), FRAG_TAG_DATE_PICKER);
            }
        });
    }

    private void initSmallRoom2() {
        List<Meeting> meetingList = new ArrayList<Meeting>();
        lv_small_room2 = (NoScrollListView) findViewById(R.id.lv_small_room2);
        lv_small_room2.setAdapter(new RoomAdapter(meetingList));

        View headView = LayoutInflater.from(this).inflate(R.layout.meeting_list_title, null);
        TextView tv_title = (TextView) headView.findViewById(R.id.tv_room_name);
        TextView tv_local = (TextView) headView.findViewById(R.id.tv_local);
        tv_title.setText("小会议室2(前台左边)");
        tv_local.setText("分机号：101");
        lv_small_room2.addHeaderView(headView);
    }

    private void initSmallRoom1() {
        List<Meeting> meetingList = new ArrayList<Meeting>();
        meetingList.add(new Meeting("张婧桦", "面试", "09:30", "10:30"));
        meetingList.add(new Meeting("张婧桦", "面试", "10:30", "11:30"));
        meetingList.add(new Meeting("张婧桦", "面试", "13:30", "14:30"));
        meetingList.add(new Meeting("张婧桦", "面试", "15:30", "16:30"));
        meetingList.add(new Meeting("张婧桦", "面试", "16:30", "17:30"));
        meetingList.add(new Meeting("张婧桦", "面试", "17:30", "18:30"));
        lv_small_room1 = (NoScrollListView) findViewById(R.id.lv_small_room1);
        lv_small_room1.setAdapter(new RoomAdapter(meetingList));

        View headView = LayoutInflater.from(this).inflate(R.layout.meeting_list_title, null);
        TextView tv_title = (TextView) headView.findViewById(R.id.tv_room_name);
        TextView tv_local = (TextView) headView.findViewById(R.id.tv_local);
        tv_title.setText("小会议室1(正门左边)");
        tv_local.setText("分机号：101");
        lv_small_room1.addHeaderView(headView);

    }

    private void initMiddleRoom() {
        List<Meeting> meetingList = new ArrayList<Meeting>();
        meetingList.add(new Meeting("葛仪豪", "URS评审会议", "09:30", "10:30"));
        lv_middle_room = (NoScrollListView) findViewById(R.id.lv_middle_room);
        lv_middle_room.setAdapter(new RoomAdapter(meetingList));

        View headView = LayoutInflater.from(this).inflate(R.layout.meeting_list_title, null);
        TextView tv_title = (TextView) headView.findViewById(R.id.tv_room_name);
        TextView tv_local = (TextView) headView.findViewById(R.id.tv_local);
        tv_title.setText("中会议室(更衣室和小峰房间中间)");
        tv_local.setText("分机号：101");
        lv_middle_room.addHeaderView(headView);
    }

    private void initBigRoom() {
        List<Meeting> bigRoomList = new ArrayList<Meeting>();
        bigRoomList.add(new Meeting("刘正龙", "与总部会议", "09:30", "10:30"));
        lv_big_room = (NoScrollListView) findViewById(R.id.lv_big_room);
        lv_big_room.setAdapter(new RoomAdapter(bigRoomList));

        View headView = LayoutInflater.from(this).inflate(R.layout.meeting_list_title, null);
        TextView tv_title = (TextView) headView.findViewById(R.id.tv_room_name);
        TextView tv_local = (TextView) headView.findViewById(R.id.tv_local);
        tv_title.setText("大会议室(前台右边)");
        tv_local.setText("分机号：101");
        lv_big_room.addHeaderView(headView);
    }


    //添加会议室预约
    View.OnClickListener addMeeting = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MeetingActivity.this, MeetingFormActivity.class);
            startActivity(intent);
        }
    };

    class RoomAdapter extends BaseAdapter {

        private List<Meeting> meetingList;

        public RoomAdapter(List<Meeting> meetingList) {
            this.meetingList = meetingList;
        }

        @Override
        public int getCount() {
            return meetingList.size();
        }

        @Override
        public Meeting getItem(int position) {
            return meetingList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Meeting meetingItem = getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(MeetingActivity.this).inflate(R.layout.item_meeting2, null);
            }

            TextView tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            TextView tv_content = (TextView) convertView.findViewById(R.id.tv_content);

            tv_time.setText(meetingItem.getStartTime() + "~" + meetingItem.getEndTime());
            tv_content.setText(meetingItem.getBookName() + "\n" + meetingItem.getTitle());

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MeetingActivity.this, MeetingFormActivity.class);
                    startActivity(intent);
                }
            });

            return convertView;
        }
    }

    /**
     * 获取指定日期是星期几
     * 参数为null时表示获取当前日期是星期几
     *
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        String[] weekOfDays = {"日", "一", "二", "三", "四", "五", "六"};
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekOfDays[w];
    }
}
